<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="宣讲会名称" prop="lectureName" label-width="100px">
        <el-input
          v-model="queryParams.lectureName"
          placeholder="请输入宣讲会名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宣讲会开始时间" prop="lectureStarttime" label-width="120px">
        <el-date-picker
          v-model="queryParams.lectureStarttime"
          clearable
          size="small"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择宣讲会开始时间"
        />
      </el-form-item>
      <el-form-item label="宣讲会举办场地" prop="lecturePlace" label-width="120px">
        <el-input
          v-model="queryParams.lecturePlace"
          placeholder="请输入宣讲会举办场地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['zua:lecture:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['zua:lecture:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['zua:lecture:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['zua:lecture:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="lectureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="就业信息名称" align="center" prop="recruitId" />
      <el-table-column label="宣讲会名称" align="center" prop="lectureName" show-overflow-tooltip />
      <el-table-column label="宣讲会开始时间" align="center" prop="lectureStarttime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lectureStarttime, '{y}-{m}-{d} {hh}:{mm}:{ss}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="宣讲会结束时间" align="center" prop="lectureStoptime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lectureStoptime, '{y}-{m}-{d} {hh}:{mm}:{ss}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="宣讲会举办场地" align="center" prop="lecturePlace" />
      <el-table-column label="宣讲会联系人" align="center" prop="lectureContacts" />
      <el-table-column label="宣讲会联系电话" align="center" prop="lectureTell" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['zua:lecture:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['zua:lecture:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改宣讲会对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="就业信息名称" prop="recruitId" label-width="120px">
          <el-select v-model="form.recruitId" placeholder="请选择就业信息名称">
            <el-option
              v-for="item in recruitList"
              :key="item.recruitId"
              :label="item.recruitName"
              :value="item.recruitName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="宣讲会名称" prop="lectureName" label-width="120px">
          <el-input v-model="form.lectureName" placeholder="请输入宣讲会名称" />
        </el-form-item>
        <el-form-item label="宣讲会开始时间" prop="lectureStarttime" label-width="120px">
          <el-date-picker
            v-model="form.lectureStarttime"
            clearable
            size="small"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择宣讲会开始时间"
          />
        </el-form-item>
        <el-form-item label="宣讲会结束时间" prop="lectureStoptime" label-width="120px">
          <el-date-picker
            v-model="form.lectureStoptime"
            clearable
            size="small"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择宣讲会结束时间"
          />
        </el-form-item>
        <el-form-item label="宣讲会举办机构" prop="lectureHold" label-width="120px">
          <el-input v-model="form.lectureHold" placeholder="请输入宣讲会举办机构" />
        </el-form-item>
        <el-form-item label="宣讲会举办场地" prop="lecturePlace" label-width="120px">
          <el-input v-model="form.lecturePlace" placeholder="请输入宣讲会举办场地" />
        </el-form-item>
        <el-form-item label="宣讲会联系人" prop="lectureContacts" label-width="120px">
          <el-input v-model="form.lectureContacts" placeholder="请输入宣讲会联系人" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="lectureTell" label-width="120px">
          <el-input v-model="form.lectureTell" placeholder="请输入宣讲会联系电话" />
        </el-form-item>
        <el-form-item label="公司地址" prop="lectureAddress" label-width="120px">
          <el-input v-model="form.lectureAddress" placeholder="请输入公司地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLecture, getLecture, delLecture, addLecture, updateLecture, exportLecture } from '@/api/zua/lecture'
import { listRecruit } from '@/api/zua/recruit'
export default {
  name: 'Lecture',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宣讲会表格数据
      lectureList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recruitId: null,
        lectureName: null,
        lectureStarttime: null,
        lectureStoptime: null,
        lectureHold: null,
        lecturePlace: null,
        lectureContacts: null,
        lectureTell: null,
        lectureAddress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //
      recruitList: {}
    }
  },
  async created() {
    await this.getRecruitList()
    await this.getList()
    this.lectureList.map(item => {
      this.recruitList.map(item1 => {
        if (item.recruitId === item1.recruitId) {
          item.recruitId = item1.recruitName
        }
      })
    })
  },
  methods: {
    // 查询所有的招聘信息列表
    async getRecruitList() {
      await listRecruit().then(res => {
        this.recruitList = res.rows
      })
    },
    /** 查询宣讲会列表 */
    async getList() {
      this.loading = true
      await listLecture(this.queryParams).then(response => {
        this.lectureList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        lectureId: null,
        recruitId: null,
        lectureName: null,
        lectureStarttime: null,
        lectureStoptime: null,
        lectureHold: null,
        lecturePlace: null,
        lectureContacts: null,
        lectureTell: null,
        lectureAddress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lectureId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加宣讲会'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const lectureId = row.lectureId || this.ids
      getLecture(lectureId).then(response => {
        this.form = response.data
        this.recruitList.map(item => {
          if (this.form.recruitId === item.recruitId) {
            this.form.recruitId = item.recruitName
          }
        })
        this.open = true
        this.title = '修改宣讲会'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.recruitList.map(item => {
            if (this.form.recruitId === item.recruitName) {
              this.form.recruitId = item.recruitId
            }
          })
          if (this.form.lectureId != null) {
            // console.log(this.form)
            updateLecture(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addLecture(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lectureIds = row.lectureId || this.ids
      this.$confirm('是否确认删除宣讲会编号为"' + lectureIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delLecture(lectureIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有宣讲会数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportLecture(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
